// pages/myComments/myComments.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentData: 2,
    goodslistOne: [],
    goodslistTwo: [],
    goodslistThree: [],
    noData: [true, true, true],
    page: 1,
    frequency: true,
  },

  toDetail(e) {
    const that = this;
    let id = e.currentTarget.dataset.goodsid;
    let type = e.currentTarget.dataset.promtype;
    let teamId = e.currentTarget.dataset.promid;
    // if (type == '6'){
    //   wx.navigateTo({
    //     url: `/page/detail/pages/assembleDetails/assembleDetails?id=${id}&teamid=${teamId}&from=1`
    //   })
    // }else{
    wx.navigateTo({
      url: `/page/detail/pages/details/details?id=${id}`,
    })
    // }
  },


  //获取当前滑块的index
  bindchange: function (e) {
    const that = this;

    that.setData({
      currentData: e.detail.current,
      page: 1,
      frequency: true
    })

  },
  //点击切换，滑块index赋值
  checkCurrent: function (e) {
    const that = this;

    if (that.data.currentData === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentData: e.target.dataset.current,
        page: 1,
        frequency: true
      })
      that.getData();
    }
    return false
  },


  getData() {
    const that = this;
    let showIndex = that.data.currentData
    let page = 1;
    let json = {
      status: showIndex,
      token: app.globalData.token,
      p: page
    }
    let jsonData = app.utils.getSignData(json);
    that.setData({
      hiddenLoading: false
    })
    app.utils.post(app.api.reqUrls.commentsList, jsonData).then(res => {
      that.setData({
        hiddenLoading: true
      })
      if (res.status == 1) {
        // 格式化时间
        res.result.forEach((item, index) => {
          item.img = app.api.reqUrls.getThumImages + "?width=200&height=200&goods_id=" + item.goods_id
          item.ctime = app.moment(item.add_time * 1000).format('YYYY-MM-DD');
          // item.ctime = app.moment(item.ctime).fromNow();
        })
        if (showIndex == 0) {
          if (res.result == '') {
            that.setData({
              'noData[0]': false
            })
          } else {
            res.result.map((res) => {
              res.status = false
            })
            that.setData({
              'noData[0]': true,
              goodslistOne: res.result
            })
          }
        } else if (showIndex == 1) {
          if (res.result == '') {
            that.setData({
              'noData[1]': false
            })
          } else {
            res.result.map((res) => {
              res.status = true
            })
            that.setData({
              'noData[1]': true,
              goodslistTwo: res.result
            })
          }
        } else if (showIndex == 2) {
          if (res.result == '') {
            that.setData({
              'noData[2]': false
            })
          } else {
            res.result.map((res) => {
              if (res.is_comment == 1) {
                res.status = true
              } else {
                res.status = false
              }
            })
            that.setData({
              'noData[2]': true,
              goodslistThree: res.result
            })
          }
        }

      } else {
        throw res.msg
      }
    }).catch(err => {
      that.setData({
        hiddenLoading: true
      })
      app.showInfo(err.toString())
    })
  },
  // 评价页
  toEvaluate(e) {
    wx.navigateTo({
      url: `/page/detail/pages/evaluate/evaluate?goodsid=${e.currentTarget.dataset.goodsid}&orderid=${e.currentTarget.dataset.orderid}&recid=${e.currentTarget.dataset.recid}`

    })
  },
  // 评价详情页
  toCommentDetails(e) {
    wx.navigateTo({
      url: `/page/detail/pages/details/details?id=${e.currentTarget.dataset.goodsid}&orderid=comment`

    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    if (options.index != undefined) {
      that.setData({
        currentData: options.index
      })
    }
    wx.getSystemInfo({
      success(res) {
        that.setData({
          scrollHeight: res.windowHeight - 80 / 750 * res.windowWidth
        });
      }
    });

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this;
    that.getData();
  },
  // 下拉加载数据
  upData(index) {
    const that = this;
    if (!that.data.frequency) {
      return false;
    }
    let page = that.data.page;
    page += 1;
    let json = {
      status: index - 1,
      token: app.globalData.token,
      p: page
    }
    let jsonData = app.utils.getSignData(json);
    that.setData({
      hiddenLoading: false
    })
    app.utils.post(app.api.reqUrls.commentsList, jsonData).then(res => {
      that.setData({
        hiddenLoading: true
      })
      if (res.status == 1) {
        // 格式化时间
        res.result.forEach((item) => {
          item.img = app.api.reqUrls.getThumImages + "?width=200&height=200&goods_id=" + item.goods_id
          item.ctime = app.moment(item.add_time * 1000).format('YYYY-MM-DD');
          // item.ctime = app.moment(item.ctime).fromNow();
        })


        if (res.result == '') {
          wx.showToast({
            title: '没有更多了',
            icon: "none",
            duration: 1000
          })
          that.setData({
            frequency: false
          })
          return false;
        } else {

          if (index == 0) {
            let goodslistOne = that.data.goodslistOne;
            res.result.map((res) => {
              if (res.is_comment == 0) {
                res.status = false
              } else {
                res.status = true
              }
              goodslistOne.push(res)
            })

            that.setData({
              goodslistOne,
              page: page
            })
          } else if (index == 1) {
            let goodslisTwo = that.data.goodslistTwo;
            res.result.map((res) => {
              if (res.is_comment == 0) {
                res.status = false
              } else {
                res.status = true
              }
              goodslisTwo.push(res)
            })
            that.setData({
              goodslisTwo,
              page: page
            })
          } else if (index == 2) {
            let goodslistThree = that.data.goodslistThree;
            res.result.map((res) => {
              if (res.is_comment == 0) {
                res.status = false
              } else {
                res.status = true
              }
              goodslistThree.push(res)
            })
            that.setData({
              goodslistThree,
              page: page
            })
          } else {
            return false;
          }

        }

      } else {
        throw res.msg
      }
    }).catch(err => {
      that.setData({
        hiddenLoading: true
      })
      app.showInfo(err.toString())
    })
  },
  tolower() {
    const that = this;

    that.upData(0);

  },
  tolower1() {
    const that = this;
    that.upData(1);

  },
  tolower2() {
    const that = this;
    that.upData(2);

  }
})